﻿using System;
using System.IO;
using System.Xml;
using YArchitech.LIB;

namespace YArchitech.DataModel
{
	public class UserSetting
	{
		public UserSetting()
		{
			this.filePath = Path.Combine(Product.UserDataLocation, "HYAirSystemSetting.xml");
			if (!File.Exists(this.filePath))
			{
				File.Copy(Path.Combine(Product.DataLocation, "HYAirSystemSetting.xml"), this.filePath);
			}
			this.xmlDoc = new XmlDocument();
			this.xmlDoc.Load(this.filePath);
			this.xmlNodeRoot = this.xmlDoc.FirstChild.NextSibling;
		}

		public string GetValue(string parentNode, string subNode)
		{
			string result;
			try
			{
				result = this.xmlNodeRoot.SelectSingleNode(parentNode).SelectSingleNode(subNode).InnerText;
			}
			catch (Exception)
			{
				result = "";
			}
			return result;
		}

		public void WriteValue(string parentNode, string subNode, string text)
		{
			try
			{
				XmlNode xmlNode = this.xmlNodeRoot.SelectSingleNode(parentNode);
				if (xmlNode == null)
				{
					xmlNode = this.xmlDoc.CreateElement(parentNode);
					this.xmlNodeRoot.AppendChild(xmlNode);
				}
				XmlNode xmlNode2 = xmlNode.SelectSingleNode(subNode);
				if (xmlNode2 != null)
				{
					xmlNode2.InnerText = text;
				}
				else
				{
					XmlNode xmlNode3 = this.xmlDoc.CreateElement(subNode);
					xmlNode3.InnerText = text;
					xmlNode.AppendChild(xmlNode3);
				}
				this.xmlDoc.Save(this.filePath);
			}
			catch (Exception)
			{
			}
		}

		public void ReloadFile()
		{
			try
			{
				this.xmlDoc.Load(this.filePath);
				this.xmlNodeRoot = this.xmlDoc.FirstChild.NextSibling;
			}
			catch (Exception)
			{
			}
		}

		private XmlDocument xmlDoc;

		private string filePath;

		private XmlNode xmlNodeRoot;
	}
}
